package org.unt.mail.sender.other;

import java.io.IOException;

import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;

public class MailUtil {

    private static Logger logger = Logger.getLogger("mail");

    public static String toString(MimeMessage message) {

        StringBuffer buffer = new StringBuffer();
        buffer.append("To [");

        try {

            Address[] addresses = message.getAllRecipients();

            for (int i = 0; i < addresses.length; i++) {
                buffer.append(addresses[i] + ",");
            }

            buffer.delete(buffer.length() - 1, buffer.length());
            buffer.append("] Subject [" + message.getSubject() + "] Text [");

            String content = null;

            try {
                content = (String) message.getContent();
            } catch (IOException e) {
                // logger.error(ThrowableUtil.toString(e));
            }

            buffer.append(content != null ? content.replace('\n', ' ') + "]" : "]");

        } catch (MessagingException e) {
            // logger.error(ThrowableUtil.toString(e));
        }

        return buffer.toString();
    }
}
